<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:doc="livcos.org/ns/doc"
  xmlns="http://www.w3.org/1999/XSL/Format"
  exclude-result-prefixes="doc">

  <xsl:template match="doc:table">
    <table table-layout="fixed" width="100%" margin-left="0.6cm">
      <xsl:variable name="numCols" select="count(doc:body/doc:row[1]/doc:cell)"/>
      <xsl:apply-templates select="doc:body/doc:row[1]/doc:cell" mode="columnDecl"/>
      <xsl:if test="(normalize-space(doc:title) != '') or doc:head/doc:row">
        <table-header font-weight="bold">
          <xsl:if test="normalize-space(doc:title) != ''">
            <table-row>
              <table-cell number-columns-spanned="{$numCols}" border-style="solid"
                border-width="thin" padding="2pt">
                <block margin-left="0pt" text-align="center">
                  <xsl:value-of select="doc:title" />
                </block>
              </table-cell>
            </table-row>
          </xsl:if>
          <xsl:apply-templates select="doc:head/doc:row"/>
        </table-header>
      </xsl:if>
      <xsl:if test="doc:foot/doc:row">
        <table-footer>
          <xsl:apply-templates select="doc:foot/doc:row"/>
        </table-footer>
      </xsl:if>    
      <table-body>
        <xsl:apply-templates select="doc:body/doc:row"/>
      </table-body>
    </table>
    <xsl:if test="normalize-space(doc:note) != ''">
      <block font-size="7pt" margin-left="0.8cm" margin-top="1mm">
        <xsl:apply-templates select="doc:note/node()" />
      </block>
    </xsl:if>
  </xsl:template>
  
  <xsl:template match="doc:cell" mode="columnDecl">
    <table-column column-width="{100 div count(../doc:cell)}%"/>
  </xsl:template>
  
  <xsl:template match="doc:row">
    <table-row>
      <xsl:apply-templates/>
    </table-row>
  </xsl:template>
  
  <xsl:template match="doc:cell">
    <table-cell padding="2pt" border-style="solid" border-width="thin">
      <xsl:if test="@colspan &gt; 1">
        <xsl:attribute name="number-columns-spanned">
          <xsl:value-of select="@colspan" />
        </xsl:attribute>
      </xsl:if>
      <xsl:if test="@rowspan &gt; 1">
        <xsl:attribute name="number-rows-spanned">
          <xsl:value-of select="@rowspan" />
        </xsl:attribute>
      </xsl:if>
      <block margin-left="-0.6cm">
        <xsl:value-of select="."/>
      </block>
    </table-cell>
  </xsl:template>

</xsl:stylesheet>
